package com.xlh.virtualization.service.impl;

import com.xlh.aop.PreAcquireLock;
import com.xlh.virtualization.dao.ConfigMapper;
import com.xlh.virtualization.dao.ContainerDeleteConfigMapper;
import com.xlh.virtualization.dao.helper.ConfigMapperHelper;
import com.xlh.virtualization.dao.helper.ContainerDeleterConfigMapperHelper;
import com.xlh.virtualization.domain.Config;
import com.xlh.virtualization.domain.ContainerDeleteConfig;
import com.xlh.virtualization.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static com.xlh.virtualization.constant.ConfigConstant.*;

/**
 * @author cheer
 */
@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    private ConfigMapperHelper configMapperHelper;

    @Autowired
    private ConfigMapper configMapper;

    @Autowired
    private ContainerDeleterConfigMapperHelper containerDeleterConfigMapperHelper;

    @Autowired
    private ContainerDeleteConfigMapper containerDeleteConfigMapper;

    /**
     * 查询系统全局容器销毁开关
     */
    @Override
    public Boolean getContainerDeletedSwitch() {
        Config config = configMapperHelper.getConfig();
        if (config != null && config.getSystemContainerDeletedSwitch() != null) {
            return config.getSystemContainerDeletedSwitch();
        }
        return DEFAULT_CONTAINER_DELETED_SWITCH;
    }

    /**
     * 更新系统全局容器销毁开关
     */
    @Override
    @PreAcquireLock(value = SAVE_CONFIG_KEY)
    public void updateContainerDeletedSwitch(Boolean switchOn) {
        Config config = configMapperHelper.getConfig();
        if (config == null) {
            Config insertConfig = Config.builder()
                    .systemContainerDeletedSwitch(switchOn)
                    .build();
            configMapper.insertSelective(insertConfig);
        } else if (config.getSystemContainerDeletedSwitch() != switchOn) {
            Config updateConfig = Config.builder()
                    .id(config.getId())
                    .systemContainerDeletedSwitch(switchOn)
                    .build();
            configMapper.updateByPrimaryKeySelective(updateConfig);
        }
    }

    /**
     * 查询指定课程容器销毁开关
     */
    @Override
    public Boolean getCourseContainerDeletedSwitch(Long courseId) {
        ContainerDeleteConfig containerDeleteConfig = containerDeleterConfigMapperHelper.getConfig(courseId);
        if (containerDeleteConfig != null && containerDeleteConfig.getSwitchOn() != null) {
            return containerDeleteConfig.getSwitchOn();
        }
        return getContainerDeletedSwitch();
    }

    /**
     * 更新指定课程容器销毁开关
     */
    @Override
    @PreAcquireLock(value = SAVE_COURSE_CONFIG_KEY)
    public void updateCourseContainerDeletedSwitch(Long courseId, Boolean switchOn) {
        ContainerDeleteConfig containerDeleteConfig = containerDeleterConfigMapperHelper.getConfig(courseId);
        if (containerDeleteConfig == null) {
            ContainerDeleteConfig insertContainerDeleteConfig = ContainerDeleteConfig.builder()
                    .courseId(courseId)
                    .switchOn(switchOn)
                    .build();
            containerDeleteConfigMapper.insertSelective(insertContainerDeleteConfig);
        } else if (containerDeleteConfig.getSwitchOn() != switchOn) {
            ContainerDeleteConfig updateContainerDeleteConfig = ContainerDeleteConfig.builder()
                    .id(containerDeleteConfig.getId())
                    .switchOn(switchOn)
                    .build();
            containerDeleteConfigMapper.updateByPrimaryKeySelective(updateContainerDeleteConfig);
        }
    }

    /**
     * 查询环境实训容器销毁开关
     */
    @Override
    public Boolean getTrainingContainerDeletedSwitch() {
        Config config = configMapperHelper.getConfig();
        if (config == null) {
            return DEFAULT_CONTAINER_DELETED_SWITCH;
        }
        if (config.getTrainingContainerDeletedSwitch() != null) {
            return config.getTrainingContainerDeletedSwitch();
        } else if (config.getSystemContainerDeletedSwitch() != null) {
            return config.getSystemContainerDeletedSwitch();
        }
        return DEFAULT_CONTAINER_DELETED_SWITCH;
    }

    /**
     * 更新环境实训容器销毁开关
     */
    @Override
    @PreAcquireLock(value = SAVE_CONFIG_KEY)
    public void updateTrainingContainerDeletedSwitch(Boolean switchOn) {
        Config config = configMapperHelper.getConfig();
        if (config == null) {
            Config insertConfig = Config.builder()
                    .trainingContainerDeletedSwitch(switchOn)
                    .build();
            configMapper.insertSelective(insertConfig);
        } else if (config.getTrainingContainerDeletedSwitch() != switchOn) {
            Config updateConfig = Config.builder()
                    .id(config.getId())
                    .trainingContainerDeletedSwitch(switchOn)
                    .build();
            configMapper.updateByPrimaryKeySelective(updateConfig);
        }
    }

    @Override
    public Integer getImageQuota() {
        Config config = configMapperHelper.getConfig();
        if (config != null && config.getImageQuota() != null) {
            return config.getImageQuota();
        }
        return DEFAULT_IMAGE_QUOTA;
    }

    @Override
    @PreAcquireLock(value = SAVE_CONFIG_KEY)
    public void updateImageQuota(Integer imageQuota) {
        Config config = configMapperHelper.getConfig();
        if (config == null) {
            Config insertConfig = Config.builder()
                    .imageQuota(imageQuota)
                    .build();
            configMapper.insertSelective(insertConfig);
        } else if (!Objects.equals(config.getImageQuota(), imageQuota)) {
            Config updateConfig = Config.builder()
                    .id(config.getId())
                    .imageQuota(imageQuota)
                    .build();
            configMapper.updateByPrimaryKeySelective(updateConfig);
        }
    }

}
